package com.lucenly.spell.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding

/**
 *  Created by Yang on 2020/12/21 0021 15:29.
 *  不用反射提高效率
 */
abstract class BaseFragment<VB : ViewBinding>(@LayoutRes val layoutId: Int) : Fragment(layoutId) {

    protected lateinit var binding: VB

    protected lateinit var mActivity: BaseActivity<*>

    /**
     * 获得全局的，防止使用getActivity()为空
     * @param context
     */
    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as BaseActivity<*>
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = getViewBinding(inflater, container)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        startObserve()
        initData()
        initEvent()
        super.onViewCreated(view, savedInstanceState)
    }

    protected abstract fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): VB
    protected abstract fun initData()
    protected abstract fun initEvent()
    protected abstract fun startObserve()

    override fun onStart() {
        super.onStart()
    }

    override fun onStop() {
        super.onStop()
    }

    override fun onDestroyView() {
        super.onDestroyView()
    }

}